"use strict";
//引入第三方模块
const nodemailer = require("nodemailer");

function sendMail(mail,type) {
	let code = generateCode()
	let htmlCode = `<H1>个人工具箱验证码</H1><p>有效期为5分钟</p><h2>${code}</h2>`

	// 创建发送邮件的对象
	let transporter = nodemailer.createTransport({
		//node_modules/nodemailer/lib/well-known/services.json  查看相关的配置，如果使用qq邮箱，就查看qq邮箱的相关配置
		host: "smtp.qq.com",
		port: 465,
		secure: true, // true for 465, false for other ports
		auth: {
			//发送者邮箱
			user: '1650432983@qq.com', // generated ethereal user
			//pass 不是邮箱账户的密码而是stmp的授权码（必须是相应邮箱的stmp授权码）
			//邮箱---设置--账户--POP3/SMTP服务 开启--成功开启POP3/SMTP服务,在第三方客户端登录时，密码框请输入以下授权码：
			pass: 'oejibamllhlgdehb'
		}
	});

	// 邮件的相关信息
	let mailOption = {
		//发送者邮箱
		from: '1650432983@qq.com', // sender address
		//接收者邮箱，多个邮箱用逗号间隔
		to: mail, // list of receivers
		//邮件标题
		subject: "个人工具箱邮箱注册验证",
		//文件内容，发送文件是text格式或者html格式，二者只能选择一个
		// text: "Hello world?", // plain text body
		html: htmlCode
	}
	// 发送邮件
	let result = transporter.sendMail(mailOption).then(mailRes => {
		return setVerifyCode(mail, code,type)
	})
	return result
}

function generateCode() {
	const randomStr = '00000' + Math.floor(Math.random() * 1000000)
	let code = randomStr.substring(randomStr.length - 6)
	return code
}

async function setVerifyCode(email, code,type) {
	let expiresIn = 5 * 60 *1000
	let result = await uniCloud.callFunction({
		name: 'user-center',
		data: {
			action: 'setVerifyCode',
			params: {
				email: email,
				code: code,
				expiresIn,
				type: type
			}
		}
	})
	return result
}

module.exports = sendMail;
